#!/bin/bash
source $SCRIPT_DIR/build_proxy_cfg.sh
source $SCRIPT_DIR/shlog.sh


sigterm_handler() {
  echo "Shutdown signal received."
  exit 1
}

## Setup signal trap
trap 'trap " " SIGINT SIGTERM SIGHUP; kill 0; wait; sigterm_handler' SIGINT SIGTERM SIGHUP

if [ "$BuildNotifyFs" == "true" -a "$BuildUpload2Pgy" == "true" ]; then
  if [ "$BuildAnd" == "true" ]; then
    buildLog "############# begin fs notify #############"
    cd $SCRIPT_DIR
    pwd
    if [[ (-n $AND_APK_QRCODE_FILE_PATH)]]; then
      buildLog "BuildNotifyFs::AND_APK_QRCODE_FILE_PATH = $AND_APK_QRCODE_FILE_PATH"
      python3 helper4FsNotify.py --NotifyType=${NotifyType} --BuildDesc="$BuildDesc" --JenkinsBuildDescFile="$JenkinsBuildDescFile" --AndPath="$AND_APK_QRCODE_FILE_PATH"
    elif [[ (-n $AND_APK_QRCODE_FILE_PATH) ]]; then
      buildLog "BuildNotifyFs::AND_APK_QRCODE_FILE_PATH = $AND_APK_QRCODE_FILE_PATH"
      python3 helper4FsNotify.py --NotifyType=${NotifyType} --BuildDesc="$BuildDesc" --JenkinsBuildDescFile="$JenkinsBuildDescFile" --AndPath="$AND_APK_QRCODE_FILE_PATH"
    else
      echo "<font color='red'>(Build failed!) or (Build cancel!)</font>" >> "$JenkinsBuildDescFile"
      python3 helper4FsErrNotify.py --NotifyType=${NotifyType} --BuildDesc="$BuildDesc" --JenkinsBuildDescFile="$JenkinsBuildDescFile"
    fi
    buildLog "############# end fs notify #############"
  fi
fi

if [ "$BuildNotifyEmail" == "true" -a "$BuildUpload2Pgy" == "true" ]; then
  if [ "$BuildAnd" == "true" ]; then
    buildLog "############# begin email notify #############"
    cd $SCRIPT_DIR
    pwd
    if [[ (-n $AND_APK_QRCODE_FILE_PATH)]]; then
      buildLog "BuildNotifyEmail::AND_APK_QRCODE_FILE_PATH = $AND_APK_QRCODE_FILE_PATH"
      python3 helper4SendEmail.py --EmailTo="$EmailTo" --BuildDesc="$BuildDesc" --JenkinsBuildDescFile="$JenkinsBuildDescFile" --AndPath="$AND_APK_QRCODE_FILE_PATH"
    elif [[ (-n $AND_APK_QRCODE_FILE_PATH) ]]; then
      buildLog "BuildNotifyEmail::AND_APK_QRCODE_FILE_PATH = $AND_APK_QRCODE_FILE_PATH"
      python3 helper4SendEmail.py --EmailTo="$EmailTo" --BuildDesc="$BuildDesc" --JenkinsBuildDescFile="$JenkinsBuildDescFile" --AndPath="$AND_APK_QRCODE_FILE_PATH"
    fi
    buildLog "############# end email notify #############"
  fi
fi